import type { FrontProcessing } from "@/FrontProcessing";
import { vtxlyLoader } from "cax-base";
import { FIleLoadRes } from "../interface";
import { VertexPoint, VTActorRenderType } from "cax-base";
import { GyActor } from "@/FrontProcessing/Actors/GyActor";

export class ModelComp {
  engine: FrontProcessing;
  renderer: any;
  bomInfo = [];

  constructor(engine: FrontProcessing) {
    this.engine = engine;
    this.renderer = engine.system?.app.renderer;
  }

  getInfo() {
    return this.bomInfo;
  }

  // 加载stp文件
  stp_file_load(itemDataStringArr: string | any[], result: FIleLoadRes) {
    const GeoData = [];
    // 遍历itemDataStringArr数组
    for (let i = 0; i < itemDataStringArr.length; i++) {
      // 将itemDataStringArr数组中的每一项传入vtxlyLoader构造函数，生成GeoData数组
      GeoData.push(new vtxlyLoader(itemDataStringArr[i])); // mark
    }
    // console.log("GeoData", GeoData);
    // console.log("BomInfo", result.BomInfo);
    // 将result.BomInfo传入engine.dataComp.setBomData方法
    this.engine.dataComp.setBomData(result.BomInfo);
    // 遍历GeoData数组
    GeoData.forEach((loader: any, index: number) => {
      // 记录开始时间
      const a = performance.now();
      // 创建vertexes数组
      const vertexes: VertexPoint[] = [];
      // 将loader.vertexsMap数组中的每一项传入VertexPoint构造函数，生成vertexes数组
      const tmp = [...loader.vertexsMap];
      tmp.forEach((t) => {
        vertexes.push({ id: t[0], XYZ: t[1] });
      });
      // 创建model对象
      const model = new GyActor(loader.polyData, {
        vertexs: vertexes,
        // 将result.PartIds数组中的每一项传入GyActor构造函数
        partId: result.PartIds[index], // mark
        edgeData: loader.edgeData,
        // 将result.PartEntityIds数组中的每一项传入GyActor构造函数
        partEntityId: result.PartEntityIds[index],
      });
      // 设置model对象的渲染类型为SURFACE
      model.setRenderType(VTActorRenderType.SURFACE);
      // 将loader.faceTrianglesMap赋值给model对象的faceTrianglesMap属性
      model.faceTrianglesMap = loader.faceTrianglesMap;
      // 将loader.gyData.faceIds赋值给model对象的faceIds属性
      model.faceIds = loader.gyData.faceIds;
      console.log("model", model.partEntityId);
      this.engine.model.add(model);
    });
    this.engine.view.autoAdapt();
  }
}
